<?php
require_once("XmlControls.php");

class XmlAccordi extends XmlControls{
	
	static $instance;
	
	private function __construct() {
		
	}

	static function getInstance(){
		
		if(self::$instance == NULL){
			
			self::$instance = new XmlAccordi();
		}
		
		return self::$instance;
	}

	final public function GetData($conn,$cig,$num_progr,$num_progr_scheda) {
  //SOVRASCRIVO IL NUM_PROGR_SCHEDA PER ASSOCIARE CORRETTAMENTE I MSG DI ERRORE
  $_SESSION['XML__NUM_PROGR_SCHEDA']=$num_progr_scheda;
  
		$rs=array();

		$query_data = "SELECT ID,CIG,DATA_ACCORDO,ONERI_ACCORDO,NUMERO_RISERVE,SCHEDA_CHIUSA,
									 UTENTE_ID,DATA_CHIUSURA,NUM_PROGR,DATA_SCADENZA_INVIO,NUM_PROGR_SCHEDA,ID_SIMOG
									 FROM ACCORDI WHERE CIG='".$cig."' AND SCHEDA_CHIUSA IN ('S') AND ARCHIVIATA !='S' AND NUM_PROGR='".$num_progr."'";
		
		if ($num_progr_scheda!=""){
			$query_data.=" AND NUM_PROGR_SCHEDA='".$num_progr_scheda."'";
		}

		$rs=$conn->doquery_oracle_import($query_data, "select");

		$rs_data=array();
		$i=0;
		
		foreach($rs as $data){
		
			$rs_data[$i]["ID"]=$data["1"];
			$rs_data[$i]["CIG"]=$data["2"];
			$rs_data[$i]["DATA_ACCORDO"]=$data["3"];
			$rs_data[$i]["ONERI_ACCORDO"]=$data["4"];
			$rs_data[$i]["NUMERO_RISERVE"]=$data["5"];
			$rs_data[$i]["SCHEDA_CHIUSA"]=$data["6"];
			$rs_data[$i]["UTENTE_ID"]=$data["7"];
			$rs_data[$i]["DATA_CHIUSURA"]=$data["8"];
			$rs_data[$i]["NUM_PROGR"]=$data["9"];
			$rs_data[$i]["DATA_SCADENZA_INVIO"]=$data["10"];
			$rs_data[$i]["NUM_PROGR_SCHEDA"]=$data["11"];
			$rs_data[$i]["ID_SIMOG"]=$data["12"];
			
			$i=$i+1;
		}
		
		return $rs_data;
		
	}
	
	final public function CreateXml($rs) {

		/*
		echo "<pre>";
		print_r($rs);
		echo "</pre>";
		exit;
		*/

		$xml='<DatiAccordi>';
		$rs_data[1]=$rs["data"];
		
    foreach($rs_data as $record_key  => $record_value){

		 foreach($record_value as $record){

			$DATA_ACCORDO=$this->WorkData("DATA_ACCORDO",$record["DATA_ACCORDO"],"Y");       //presenza A* - V*
			$ONERI_DERIVANTI=$this->WorkImporto("ONERI_DERIVANTI",$record["ONERI_ACCORDO"]);
			$NUM_RISERVE=$this->WorkIntero5("NUM_RISERVE",$record["NUMERO_RISERVE"],"Y");    //presenza A* - V*
			$ID_SCHEDA_LOCALE=$this->WorkNumero("ID_SCHEDA_LOCALE",$record["ID"]);												
			$ID_SCHEDA_SIMOG=$this->WorkNumero("ID_SCHEDA_SIMOG",$record["ID_SIMOG"]); 
			
			$xml.='<AccordoBonario  tns:DATA_ACCORDO="'.$DATA_ACCORDO.'" tns:NUM_RISERVE="'.$NUM_RISERVE.'"';

			if($ONERI_DERIVANTI!=''){
				$xml.=' tns:ONERI_DERIVANTI="'.$ONERI_DERIVANTI.'"';
			}
			/*if ($ID_SCHEDA_LOCALE!=''){
				$xml.=' tns:ID_SCHEDA_LOCALE="'.$ID_SCHEDA_LOCALE.'"';
			}	
			if ($ID_SCHEDA_SIMOG!=''){
				$xml.=' tns:ID_SCHEDA_SIMOG="'.$ID_SCHEDA_SIMOG.'"';
			}*/
			/*if ($ID_SCHEDA_SIMOG!='')
			{
				$xml.=' tns:ID_SCHEDA_SIMOG="'.$ID_SCHEDA_SIMOG.'"';
			}
			else
			{		  
			   $query_data_DC ="SELECT  ID_SIMOG FROM DATI_COMUNI WHERE CIG='".$record["CIG"]."' AND NUM_PROGR='".$record["NUM_PROGR"]."'";
               $rsDC=$conn->doquery_oracle_import($query_data_DC, "select");
			  if($rsDC[1][1]=="")
			  {
                if ($ID_SCHEDA_LOCALE!='')
                {
			      $xml.=' tns:ID_SCHEDA_LOCALE="'.$ID_SCHEDA_LOCALE.'"';
			    }
		      }
		    }*/
		    $xml.=' tns:ID_SCHEDA_LOCALE="'.$ID_SCHEDA_LOCALE.'"';			
      				
			$xml.='/>';
     
     }
     
		}

		$xml.='</DatiAccordi>';

		return $xml;

	}

}
?>